package vn.edu.hut.soict.translate.utils;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import vn.edu.hut.soict.lienkate.dictionary.Dictionary;
import vn.edu.hut.soict.lienkate.dictionary.VietDictionaryReader;
import vn.edu.hut.soict.lienkate.parser.Linkage;
import vn.edu.hut.soict.lienkate.parser.NewLinkageDrawer;
import vn.edu.hut.soict.lienkate.parser.exprprune.ExpressionPruneParser;

public class LinkGrammarTester {
	private static Dictionary viDictionary;
	private ExpressionPruneParser viParser;

	static {
		VietDictionaryReader viReader = new VietDictionaryReader();
		try {
			viDictionary = viReader.read("res/dictionary/lg-vi-new/dictionary.txt");
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public LinkGrammarTester() {
		viParser = new ExpressionPruneParser(viDictionary);
	}

	public void reloadDictionary() {
		VietDictionaryReader viReader = new VietDictionaryReader();
		try {
			viDictionary = viReader.read("res/dictionary/lg-vi-new/dictionary.txt");
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		viParser = new ExpressionPruneParser(viDictionary);
	}

	public String parse(String input) {
		List<String> inputTokenList = StringUtils.parseToken(input);
		List<Linkage> inputLinkages = null;
		try {
			inputLinkages = viParser.parse(inputTokenList, 0);
			return NewLinkageDrawer.toString(inputLinkages, inputTokenList);
		}
		catch (ParseException e) {
			System.err.println(e.getMessage());
		}
		return null;
	}
}
